<%@page import="dao.LessonDao"%>
<%@page import="models.User"%>
<%@include file="template/configuration.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || Manage lessons</title>
        ${stylesheet}
        ${jquery}
        ${jsdropdown}
        ${jscollapse}  
        ${multipleFilter}
        ${jspagination}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>Manage lessons</h1>
                <label for="search">Search:</label> <input type="text" id="multiplefilter" value=""/>
                <div class="clear margin-bottom-10"></div>

                <c:choose>
                    <c:when test="${Lessons!=0}">
                        <%
                            HibernateUtil huml = new HibernateUtil();
                            CourseDao cdmc = new CourseDao(huml.openSession());
                            HttpSession sessie = request.getSession();
                            List<Course> courseListMC = null;
                            if (sessie.getAttribute("userrole").equals("Administrator")) {
                                courseListMC = cdmc.findAll();
                            } else {
                                User user = (User) sessie.getAttribute("user");
                                courseListMC = cdmc.findAllByTeacher(user.getMember_id());
                            }
                            request.setAttribute("courses", courseListMC);
                            if (courseListMC.size() > 0 || sessie.getAttribute("userrole").equals("Administrator")) {
                        %>
                        <c:forEach items="${courses}" var="course">
                            <div class="bundle">
                                <div class="top">
                                    <div class="left">
                                    </div>
                                    <div class="center">
                                        <h5>${course.name}</h5>
                                    </div>
                                    <div class="right">
                                        <a href="${url}ManageLesson?courseid=${course.id}">Add a new lesson<img src="${url}images/add.png" /></a>
                                    </div>
                                </div>
                                <!-- clear //-->
                                <div class="clear"></div>            
                                <div class="full-block">
                                    <c:set var="checkempty" value="true"/>
                                    <c:forEach items="${lessonList}" var="Lessons">
                                        <c:if test="${course.id == Lessons.course.id}">
                                            <c:set var="checkempty" value="false"/>
                                        </c:if>                                    
                                    </c:forEach>
                                    <c:choose>
                                        <c:when test="${checkempty == false}">
                                            <div>
                                                <table id="search-table" class="overview">
                                                    <thead>
                                                        <tr>
                                                            <th>Name</th>
                                                            <th>Description</th>
                                                            <th class="date">Free lesson</th>
                                                            <th class="date">Live lesson</th>
                                                            <th class="date">Added</th>
                                                            <th class="single-icon">Files</th>
                                                            <th class="manage">Manage</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <c:set var="i" value="0"/>
                                                        <c:forEach items="${lessonList}" var="Lessons">
                                                            <c:if test="${course.id == Lessons.course.id}">
                                                                <c:set var="i" value="${i + 1}"/>
                                                                <tr class="<c:choose><c:when test="${i % 2 == 0}">even</c:when><c:otherwise>odd</c:otherwise></c:choose>">
                                                                    <td>
                                                                        ${Lessons.name}
                                                                    </td>
                                                                    <td>
                                                                        ${Lessons.description}
                                                                    </td>
                                                                    <td>
                                                                        <c:if test="${Lessons.freeLesson}">
                                                                            <center>X</center>
                                                                        </c:if>
                                                                    </td>
                                                                    <td>
                                                                        <c:if test="${Lessons.liveLesson}">
                                                                            <center>X</center>
                                                                        </c:if>
                                                                    </td>
                                                                    <td> 
                                                                        ${Lessons.date}
                                                                    </td>
                                                                    <td>
                                                                        <a href="${url}FileController/view?id=${Lessons.id}"><img src="${url}images/files_1.png"/></a>
                                                                    </td>
                                                                    <td>
                                                                        <a href="${url}Lesson?id=${Lessons.id}"><img src="${url}images/edit.png"/></a>
                                                                        <a href="${url}Lesson?idremove=${Lessons.id}" class="remove"><img src="${url}images/remove.png"/></a>                            
                                                                        <span class="info">Lesson <strong>${Lessons.name}</strong></span>
                                                                    </td>                                        
                                                                </tr>
                                                            </c:if>
                                                        </c:forEach> 
                                                    </tbody>
                                                </table>
                                                <c:set var="courseid" value="${course.id}" />
                                                <%
                                                HibernateUtil humls = new HibernateUtil();
                                                LessonDao cdmls = new LessonDao(humls.openSession());
                                                double total_items = cdmls.getLastPage(Long.parseLong(pageContext.getAttribute("courseid").toString()));
                                                pageContext.setAttribute("pagination", (int) Math.ceil(total_items / 15));
                                                %>
                                                <c:if test="${pagination > 1}">
                                                    <div class="pagination">
                                                        <c:forEach var="i" begin="1" end="${pagination}" step="1">
                                                            <a href="${url}jsp/pagination/manage_lessons.jsp?page=${i}&course_id=${course.id}" class="number<c:if test="${i == '1'}"> active</c:if>">${i}</a>
                                                        </c:forEach>
                                                    </div>
                                                </c:if>
                                            </div>
                                        </c:when>
                                        <c:otherwise>
                                            <div class="no-yet-message" style="display: block;">
                                                There are no lessons in this course yet.
                                            </div>
                                        </c:otherwise>
                                    </c:choose>

                                </div>    
                                <div class="clear margin-bottom-10"></div>
                            </div>
                        </c:forEach>  
                        <% } else {%>
                        <div class="full-block">    
                            there are no lessons available
                        </div>
                        <%}%>
                    </c:when>                
                    <c:otherwise>
                        <div class="full-block">    
                            there are no courses available
                        </div>
                    </c:otherwise>    
                </c:choose>
            </div>         
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
